<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * whiteboard_friend_requests
    id
    user_id
    friend_id
    response [-1, 0, 1, 2]
    timestamp
   whiteboard_friends
    id
    user_id
    friend_id
    timestamp
 */

// friend
// search - suggest
// request - cancel
// response ->  accept | deny
// remove

// get friend

function check_whiteboard_friend($user_id, $friend_id) {
    $query = "SELECT id FROM whiteboard_friends WHERE user_id = '$user_id' AND friend_id = '$friend_id'";
    $result = safe_select_query($query);
    $result = $result[0];
    $relationship = 'both';

    if(!$result) {
        $query = "SELECT id FROM whiteboard_friend_requests WHERE response = 0 AND user_id = '$user_id' AND friend_id = '$friend_id'";
        $result = safe_select_query($query);
        $result = $result[0];
        $relationship = 'to';

        if(!$result) {
            $query = "SELECT id FROM whiteboard_friend_requests WHERE response = 0 AND friend_id = '$user_id' AND user_id = '$friend_id'";
            $result = safe_select_query($query);
            $result = $result[0];
            $relationship = 'from';
            if(!$result) {
                $relationship = 'none';
            }

        }
    }
    return array(
        'guid'          => $result->id,
        'relationship'  => $relationship
    );
}

function request_whiteboard_friend($user_id, $friend_id) {
    if(!$user_id || !$friend_id)
        return FALSE;
    $timestamp = time();
    $query = "INSERT INTO whiteboard_friend_requests(user_id, friend_id, timestamp)
                VALUES('$user_id', '$friend_id', '$timestamp')
                ON DUPLICATE KEY UPDATE response = 0, timestamp = '$timestamp'";
    return safe_insert_query($query);
}

function cancel_whiteboard_friend($user_id, $friend_id) {
    if(!$user_id || !$friend_id)
        return FALSE;
    $timestamp = time();
    $query = "UPDATE whiteboard_friend_requests SET response = -1, timestamp = '$timestamp'
                WHERE user_id = '$user_id' AND friend_id = '$friend_id'";
    return safe_update_query($query);
}

function deny_whiteboard_friend($user_id, $friend_id) {
    if(!$user_id || !$friend_id)
        return FALSE;
    $timestamp = time();
    $query = "UPDATE whiteboard_friend_requests SET response = 2, timestamp = '$timestamp'
                WHERE user_id = '$user_id' AND friend_id = '$friend_id'";
    return safe_update_query($query);
}

function accept_whiteboard_friend($user_id, $friend_id) {
    if(!$user_id || !$friend_id)
        return FALSE;
    $timestamp = time();
    $query = "UPDATE whiteboard_friend_requests SET response = 1, timestamp = '$timestamp'
                WHERE user_id = '$user_id' AND friend_id = '$friend_id'";
    safe_update_query($query);
    add_whiteboard_friend($user_id, $friend_id);
    add_whiteboard_friend($friend_id, $user_id);
}

function add_whiteboard_friend($user_id, $friend_id) {
    if(!$user_id || !$friend_id)
        return FALSE;
    $timestamp = time();
    $query = "INSERT INTO whiteboard_friends(user_id, friend_id, timestamp)
                VALUES('$user_id', '$friend_id', '$timestamp')
                ON DUPLICATE KEY UPDATE timestamp = '$timestamp'";
    return safe_insert_query($query);
}

function remove_whiteboard_friend($user_id, $friend_id) {
    if(!$user_id || !$friend_id)
        return FALSE;
    $query = "DELETE FROM whiteboard_friends WHERE user_id = '$user_id' AND friend_id = '$friend_id'";
    safe_delete_query($query);
    $query = "DELETE FROM whiteboard_friends WHERE friend_id = '$user_id' AND user_id = '$friend_id'";
    safe_delete_query($query);    
}

function get_whiteboard_friend_requests($user_id) {
    $query = "SELECT user_id, friend_id, response, timestamp FROM whiteboard_friend_requests WHERE friend_id = '$user_id' AND repsonse = 0";
    return safe_select_query($query);
}

function get_whiteboard_friends_of($user_id) {
    $query = "SELECT friend_id FROM whiteboard_friends WHERE user_id = '$user_id'";
    return safe_select_query($query);
}

// arg: user_id
function get_whiteboard_friend_requests_to($user_id) {
    $query = "SELECT user_id, friend_id, response, timestamp FROM whiteboard_friend_requests WHERE user_id = '$user_id' AND response = '0'";
    return safe_select_query($query);
}

// arg: user_id
function get_whiteboard_friend_requests_from($user_id) {
    $query = "SELECT user_id, friend_id, response, timestamp FROM whiteboard_friend_requests WHERE friend_id = '$user_id' AND response = '0'";
    return safe_select_query($query);
}

?>
